package com.ebelter.myehc.models.ota;


import android.content.Context;

import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.myehc.R;


import no.nordicsemi.android.dfu.DfuProgressListener;
import no.nordicsemi.android.dfu.DfuServiceInitiator;
import no.nordicsemi.android.dfu.DfuServiceListenerHelper;

//OTA 升级工具
public class OtaManager {


    private static final String TAG = "OtaManager";

    public static final String VERSION = "BY_02_H2902_0816";//手环固件常量

    private String srcMac;
    private String otaMac;
    DfuServiceInitiator starter;




    public void setSrcMac(String srcMac) {
        this.srcMac = srcMac;
    }

    public void setOtaMac(String otaMac) {
        this.otaMac = otaMac;
    }

    public String getOtaMac() {
        return otaMac;
    }

    public void registerProgressListener(Context context) {
        DfuServiceListenerHelper.registerProgressListener(context, mDfuProgressListener);
    }
    public void unRegisterProgressListener(Context context) {
        DfuServiceListenerHelper.unregisterProgressListener(context, mDfuProgressListener);
    }

    DfuProgressListener mDfuProgressListener2;

    public void setmDfuProgressListener2(DfuProgressListener mDfuProgressListener2) {
        this.mDfuProgressListener2 = mDfuProgressListener2;
    }

    private final DfuProgressListener mDfuProgressListener = new DfuProgressListener() {
        @Override
        public void onDeviceConnecting(String deviceAddress) {
            LogUtils.i(TAG, "------onDeviceConnecting deviceAddress=" + deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onDeviceConnecting(deviceAddress);
        }

        @Override
        public void onDeviceConnected(String deviceAddress) {
            LogUtils.i(TAG, "------onDeviceConnected deviceAddress="+deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onDeviceConnected(deviceAddress);
        }

        @Override
        public void onDfuProcessStarting(String deviceAddress) {
            LogUtils.i(TAG, "------onDfuProcessStarting deviceAddress="+deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onDfuProcessStarting(deviceAddress);
        }

        @Override
        public void onDfuProcessStarted(String deviceAddress) {
            LogUtils.i(TAG, "------onDfuProcessStarted deviceAddress="+deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onDfuProcessStarted(deviceAddress);
        }

        @Override
        public void onEnablingDfuMode(String deviceAddress) {
            LogUtils.i(TAG, "------onEnablingDfuMode deviceAddress=" +deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onEnablingDfuMode(deviceAddress);
        }

        @Override
        public void onProgressChanged(String deviceAddress, int percent, float speed, float avgSpeed, int currentPart, int partsTotal) {
            LogUtils.i(TAG, "------onProgressChanged" + percent+"--deviceAddress="+deviceAddress+"--percent="+percent);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onProgressChanged(deviceAddress,percent,speed,avgSpeed,currentPart,partsTotal);
        }

        @Override
        public void onFirmwareValidating(String deviceAddress) {
            LogUtils.i(TAG, "------onFirmwareValidating deviceAddress="+deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onFirmwareValidating(deviceAddress);
        }

        @Override
        public void onDeviceDisconnecting(String deviceAddress) {

            LogUtils.i(TAG, "------onDeviceDisconnecting deviceAddress="+deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onDeviceDisconnecting(deviceAddress);
        }

        @Override
        public void onDeviceDisconnected(String deviceAddress) {
            LogUtils.i(TAG, "------onDeviceDisconnected deviceAddress="+deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onDeviceDisconnected(deviceAddress);

        }

        @Override
        public void onDfuCompleted(String deviceAddress) {
            LogUtils.i(TAG, "------onDfuCompleted deviceAddress="+deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onDfuCompleted(deviceAddress);
        }

        @Override
        public void onDfuAborted(String deviceAddress) {
            LogUtils.i(TAG, "------onDfuAborted deviceAddress="+deviceAddress);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onDfuAborted(deviceAddress);
        }

        @Override
        public void onError(String deviceAddress, int error, int errorType, String message) {
            LogUtils.i(TAG, "onError deviceAddress:" + deviceAddress + "--error:" + error + "--errorType:" + errorType + "--message:" + message);
            if (mDfuProgressListener2!=null)mDfuProgressListener2.onError(deviceAddress,error,errorType,message);
        }
    };

    public void startUpdate(Context context) {
        starter = new DfuServiceInitiator(otaMac)
                //.setDeviceName("DfuTarg")
                .setKeepBond(false)
                .setPacketsReceiptNotificationsEnabled(true)
                .setPacketsReceiptNotificationsValue(10);
        // If you want to have experimental buttonless DFU feature supported call additionally:
       // starter.setUnsafeExperimentalButtonlessServiceInSecureDfuEnabled(true);
        starter.setZip(R.raw.a0816_20190524); //setDeviceName为设备过滤条件 setzip传入raw文件的路径即可
        starter.start(context, DfuService.class); //启动升级服务
    }

    public void startUpdate2(Context context){
        if (starter!=null)starter.start(context, DfuService.class); //启动升级服务
    }
}
